﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;
using System.Xml;
using Microsoft.Office.Server.Search.WebControls;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;

namespace WP.CustomSearch.MySearchBuilder
{
    [ToolboxItemAttribute(false)]

    public class MySearchBuilder : CoreResultsWebPart
    {
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Main Keyword Query to search")]
        [WebDisplayName("Fixed Query to Search")]
        [Category("My Query Builder")]
        public string MainQueryPrefix { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Mananged Property that should be searched for current user")]
        [WebDisplayName("My Query Managed Property")]
        [Category("My Query Builder")]
        public string MyQueryPrefix { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Sort by this managed property")]
        [WebDisplayName("Managed Property")]
        [Category("Sort Override")]
        public string OrderByProperty { get; set; }

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDescription("Sort direction")]
        [Category("Sort Override")]
        public Microsoft.Office.Server.Search.Query.SortDirection SortDirection { get; set; }

        protected override void ConfigureDataSourceProperties()
        {
            string QueryFormat = MainQueryPrefix + " " + MyQueryPrefix + ":\"{0}\"";

            string currentUserName = SPContext.Current.Web.CurrentUser.LoginName;
            string currentName = SPContext.Current.Web.CurrentUser.Name;

            StringBuilder sb = new StringBuilder(String.Format(QueryFormat, currentUserName));
            sb.Append(String.Format(" " + MyQueryPrefix + ":\"{0}\"", currentName));

            this.FixedQuery = sb.ToString();

            // only do stuff when search results are visible
            if (this.ShowSearchResults)
            {
                // run the base code
                base.ConfigureDataSourceProperties();
                try
                {
                    // if OrderByProperty is not set, use default behavior
                    if (!string.IsNullOrEmpty(OrderByProperty))
                    {
                        // get the datasource and change the sortorder
                        CoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource;
                        dataSource.SortOrder.Clear();
                        dataSource.SortOrder.Add(OrderByProperty, SortDirection);
                    }
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("MYWEBPART", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                }
            }
        }
    }
}
